package com.lagou.user.service;

import com.lagou.common.dao.LagouAuthCodeMapper;
import com.lagou.common.dao.LagouTokenMapper;
import com.lagou.common.entity.LagouAuthCode;
import com.lagou.common.entity.LagouToken;
import lagou.service.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.dubbo.config.annotation.Service;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-06-04.
 *
 * @author chouFy
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private LagouAuthCodeMapper lagouAuthCodeMapper;

    @Autowired
    private LagouTokenMapper lagouTokenMapper;


    public Object register(String email, String password, String code) {

        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setEmail(email);
        lagouAuthCode.setCode(code);

        lagouAuthCode = lagouAuthCodeMapper.selectOne(lagouAuthCode);

        if (lagouAuthCode != null) {
            if (lagouAuthCode.getExpiretime().getTime() < System.currentTimeMillis()) {
                lagouAuthCodeMapper.delete(lagouAuthCode);
                return false;
            }
            return true;
        }

        return false;
    }


    public Object isRegistered(String email) {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setEmail(email);
        lagouAuthCode = lagouAuthCodeMapper.selectOne(lagouAuthCode);

        if (lagouAuthCode == null) {
            return false;
        }
        return true;
    }


    public Object info(String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        lagouToken = lagouTokenMapper.selectOne(lagouToken);
        if (lagouToken != null) {
            return lagouToken.getEmail();
        }
        return null;
    }
}
